package com.yemast.navigationtest

import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.navigation.fragment.NavHostFragment
import kotlinx.android.synthetic.main.fragment_test_fragmnet2.*


// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

class TestFragmnet2 : BaseFragment() {
    private var param1: String? = null
    private var param2: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        arguments?.getString("name")?.let { Log.e("tag", "$it"); }

        arguments?.getInt("age")?.let {
            Log.e("tag", "$it");
        }

        arguments?.getInt("age1")?.let {
            Log.e("tag", "$it");
        }

        return inflater.inflate(R.layout.fragment_test_fragmnet2, container, false)
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        tv_btn2.setOnClickListener {
            NavHostFragment.findNavController(this).navigateUp()
        }
        view?.setOnKeyListener { v, keyCode, keyEvent ->
            if (keyEvent.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
                Toast.makeText(activity, "按了返回键", Toast.LENGTH_SHORT).show()
                return@setOnKeyListener true
            }
            return@setOnKeyListener false
        }

    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        Log.e("tag", "aa");
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            TestFragmnet2().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}
